package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.repository.DestinationRepositoey;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepositoey destinationRepositoey;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;
    private Pageable pageable;

    @Override
    public List<Destination> list() {
        return destinationRepositoey.findAll();
    }

    @Override
    public Destination selectOne(String id) {
        return destinationRepositoey.findById(id).orElse(null);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //创建查询对象
        Query query = new Query();
        //如果parentId有值的话就根据parentId来查询目的地,如果没有,就查询parentId为空的目的地
        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        //关键字查询
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(".*" + qo.getKeyword() + ".*"));
        }
        //页面显示集合
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, Destination.class, query, pageable);

    }


    @Override
    public Destination saveOrUpdate(Destination destination) {
        if(StringUtils.hasLength(destination.getId())){
            //根据id查询对象
            Destination destination1 = this.selectOne(destination.getId());
            //设置简介
            destination1.setInfo(destination.getInfo());
            //更新
            destinationRepositoey.save(destination1);
            return destination1;
        }else{
            return null;
        }
    }

    @Override
    public Object queryByRegionId(String rid) {
        //通过rid查询关联目的地id集合
        Region region = regionService.selectOne(rid);
        List<String> ids = region.getRefIds();
        //通过ids查询目的地对象集合
        List<Destination> dests = destinationRepositoey.findByIdIn(ids);
        return dests;
    }

    @Override
    public List<Destination> getToast(String parentId) {
        if (!StringUtils.hasLength(parentId)) {
            return Collections.emptyList();
            //没有长度则返回空集合
        }
        //创建个空的集合先
        ArrayList<Destination> list = new ArrayList<>();
        /**
         * 可以用一个方法抽取出来
         */
       /* //根据parentId查询目的地
        //广州-------------------
        Destination destination = this.selectOne(parentId);
        list.add(destination);
        //看还有没有ParentId
        if(StringUtils.hasLength(destination.getParentId())){
            //广东--------------------------
            Destination destination1 = this.selectOne(destination.getParentId());
            list.add(destination1);
            if(StringUtils.hasLength(destination1.getParentId())){
                //中国-------------------------------
                Destination destination2 = this.selectOne(destination1.getParentId());
                list.add(destination2);
            }*/
        //调用方法
        createToast(list, parentId);
        //集合元素反转
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> queryByRegionIdFor(String regionId) {
        List<Destination> list = new ArrayList();
        //如果regionId = -1 查询国内的所有省份
        if ("-1".equals(regionId)) {
            list = destinationRepositoey.findByParentName("中国");
        } else {
            //不等于-1则查询国外所有目的地
            Region region = regionService.selectOne(regionId);
            //获取关联id
            List<String> refIds = region.getRefIds();
            list = destinationRepositoey.findByIdIn(refIds);
        }

        //子目的地查询

        for (Destination d : list) {
            //jpa方法怎么分页显示
            Pageable pageable = PageRequest.of(0, 5,Sort.Direction.ASC,"_id");
            List<Destination> children = destinationRepositoey.findByParentId(d.getId(),pageable);
            //设置children集合
            d.setChildren(children);
        }
        return list;

    }

    @Override
    public void delete(String id) {
        destinationRepositoey.deleteById(id);
    }

    @Override
    public Destination get(String id) {

        return this.selectOne(id);
    }

    @Override
    public Destination getByName(String keyword) {

        return destinationRepositoey.findByName(keyword);

    }

    public void createToast(List<Destination> list, String parentId) {
        Destination destination = this.selectOne(parentId);
        list.add(destination);
        if (StringUtils.hasLength(destination.getParentId())) {
            createToast(list, destination.getParentId());
        }
    }

}

